<template>
  <div class="player-wrap">
    <div id="container" ref="container"></div>
  </div>
</template>

<script>
  import Jessibuca from "./renderer";

  export default {
    name: "JessicaPlayer",
    data() {
      return {
        jessibuca: null,
        wasm: false,
        vc: "ff",
        playing: false,
        err: ""
      }
    },
    props: {
      streamPath: {
        type: String,
        default: ''
      }
    },
    computed: {
      decoder() {
        return this.vc + (this.wasm ? "_wasm" : "") + ".js"
      }
    },
    watch: {
      'streamPath': function (newValue, oldValue) {
        if (this.jessibuca) {
          this.jessibuca.destroy();
          this.play();
        }
      }
    },
    created() {
    },
    mounted() {
      this.create();
    },
    methods: {
      create() {
        this.jessibuca = new Jessibuca({
          container: this.$refs.container,
          decoder: this.decoder,
          videoBuffer: 0
        });
        this.jessibuca.onLog = msg => (console.error(msg));
        this.play();
      },
      play() {
        if (this.streamPath) {
          let fullPath = 'ws://192.168.31.19:8080/'+this.streamPath;
          this.jessibuca.play(fullPath);
        }
      },
    },
    destroyed() {
      this.jessibuca.destroy();
    }
  }
</script>

<style scoped>

  .player-wrap {
    width: 100%;
    height: 100%;
  }

  #container {
    width: 100%;
    height: 100%;
  }
</style>
